/*
 * @(#)AttributeMethod.java        0.1 09/04/29
 * @version 	0.1 29 Apr 2009
 * @author 	Tran Dai Hiep
 * 
 * Lop truu tuong, la co so cho Attribute va Method
 */


package GUI;

import java.util.ResourceBundle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import Data.*;

public abstract class AttributeMethod extends JPanel {
	protected final Color PANEL_BACKGROUND = new Color(250, 250, 150);
	protected final Color TEXT_FIELD_BACKGROUND = new Color(250, 250, 150);
	private final int TEXT_FIELD_LENGTH = 10;
	
	ResourceBundle lang;
	Options options;
	Log log;
	Classes classes;
	
	JPanel me = this;
	ClassFrame classFrame;
	int type;
	protected JLabel typeLabel;
	protected TextField nameTextField;
	protected JButton deleteButton;
	
	public AttributeMethod(int _type) {
		options = Options.getInstance();
		log = Log.getInstance();
		lang = Options.getLanguage();
		classes = Classes.getInstance();
		type = _type;
		setLayout(new FlowLayout(FlowLayout.LEFT));
		
		// Ten cua attribute hay method
		nameTextField = new TextField(lang.getString("AttributeMethod.nameTextField.defaultText"), TEXT_FIELD_LENGTH);
		add(nameTextField);
		nameTextField.selectAll();
		nameTextField.setBorder(null);
		nameTextField.setBackground(TEXT_FIELD_BACKGROUND);
		
		// Button xoa attribute hay method
		deleteButton = new JButton(lang.getString("AttributeMethod.deleteButton.text"));
		add(deleteButton);
		deleteButton.addMouseListener(new MouseHandler());
		deleteButton.setBorder(null);
		deleteButton.setBackground(PANEL_BACKGROUND);
		
		this.setBackground(PANEL_BACKGROUND);
		this.setBorder(null);
		
		addComponentListener(new ComponentHandler());
		

		if (type == Data.Class.ABSTRACT_CLASS) {
			Font font = new Font(nameTextField.getFont().getName(), Font.ITALIC, nameTextField.getFont().getSize());
			nameTextField.setFont(font);
			font = new Font(deleteButton.getFont().getName(), Font.ITALIC + Font.BOLD, deleteButton.getFont().getSize());
			deleteButton.setFont(font);
		}
	}

	
	public AttributeMethod(String text, int type, ClassFrame classFrame) {
		this(type);
		nameTextField.setText(text);
		this.classFrame = classFrame;
	}

	
	public AttributeMethod(AttributeMethod am, ClassFrame classFrame) {
		this(am.nameTextField.getText(),am.type, classFrame);
	}
	
	public JTextField getNameReference() {
		return nameTextField;
	}
	
	public void setAbstract() {
		Font font = new Font(nameTextField.getFont().getName(), Font.ITALIC, nameTextField.getFont().getSize());
		nameTextField.setFont(font);
	}
	
	public void actionPerformed(ActionEvent event) {
		//JOptionPane.showMessageDialog(null, "123dfhdfh");
		//event.
	}

	private class MouseHandler extends MouseAdapter {
		public void mouseClicked(MouseEvent event) {
			if (event.getSource() == deleteButton) {
				deleteButton_Clicked();
			}
		}
	}
	
	private int getTextFieldLength() {
		return (classFrame.getWidth() - deleteButton.getWidth() - 10) / 12;
	}
	
	/*
	 * Thay doi kich thuoc cua attribute hay method khi resize classFrame
	 */
	private class ComponentHandler extends ComponentAdapter {
		public void componentResized(ComponentEvent event) {
			int textLength = (classFrame.getWidth() - deleteButton.getWidth() - 10) / 12;
			nameTextField.setColumns(textLength);
		}
	}
	
	//public abstract void nameTestField_EnterKeyPressed();
	
	public abstract void save();
	
	public abstract void deleteButton_Clicked();
}
